package com.mspbots.cw.amqp.receivers;

import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.cw.amqp.BaseReceiver;
import com.mspbots.cw.amqp.model.TenantSlas;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Log4j2
@Component
@RequiredArgsConstructor
public class SlasReceiver extends BaseReceiver {

    @RabbitListener(queues = "wise.slas")
    public void processMessage(JsonNode slas) {
        log.debug("wise.slas tenant {} receiver is {}", slas.get("tenantId"), slas.get("id"));
        TenantSlas tenantSlas = TenantSlas.builder()
                .respondHours(slas.get("respondHours").asDouble())
                .tenantId(slas.get("tenantId").asInt())
                .planWithin(slas.get("planWithin").asDouble())
                .resolutionHours(slas.get("resolutionHours").asDouble())
                .build();
        this.databaseClient.insert().into(TenantSlas.class).using(tenantSlas).fetch().rowsUpdated();
    }
}
